﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Common.Utlis
{
    public class DllImportHelp
    {
        /// <summary>
        /// 添加环境变量
        /// </summary>
        /// <param name="path"></param>
        public static void AddEnvironmentPaths(string path)
        {
            string currentPath = Environment.GetEnvironmentVariable("PATH");
            List<string> lisPaths=new List<string>();
            lisPaths.Add(currentPath);
            lisPaths.Add(path);
            string pathSeparator = Path.PathSeparator.ToString();
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                pathSeparator = ":";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                pathSeparator = ";";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                //todo 
            }
            string newPath = string.Join(pathSeparator, lisPaths);
            Environment.SetEnvironmentVariable("PATH", newPath);
        }

        /// <summary>
        /// 添加环境变量
        /// </summary>
        /// <param name="paths">路径列表</param>
        private static void AddEnvironmentPaths(IEnumerable<string> paths)
        {
            var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty };
            string newPath = string.Join(Path.PathSeparator.ToString(), path.Concat(paths));
            Environment.SetEnvironmentVariable("PATH", newPath);   // 这种方式只会修改当前进程的环境变量
        }
    }
}
